This page details the procedures for installing and reinstalling GRUB 2. The information applies to most versions of GRUB 2, but is specifically written for version 1.99. GRUB 1.99 is included on Ubuntu releases 11.04 (Natty Narwhal) and later. For more information on a variety of GRUB 2 topics, please visit the GRUB2 main page. For information on converting to or from GRUB Legacy, please view the Grub2/Upgrading community doc.

The GRUB 2 bootloader is included on all currently-supported versions of the Ubuntu family. GRUB 2 can accomodate traditional computer firmware such as BIOS as well as the newer EFI/UEFI standards. It is compatible with MBR, GPT and other partitioning tables.

All references to GRUB pertain to GRUB 2. Where GRUB 0.97 is discussed, it will be referred to by version number or as GRUB Legacy.

GRUB 2 Initial Installation

GRUB 2 installation is automatically incorporated into a normal Ubuntu installation. Input required by the user for the initial setup is very simple. If the installation will use the entire disk no specific GRUB selections are necessary. If the user wants to use a specific partition for Ubuntu the user must select the Ubuntu partition and the drive on which to embed the bootloader information.

The main GRUB 2 folder locations include /boot/grub/ and /etc/grub.d/. User preferences are stored in the /etc/default/grub file. The GRUB 2 menu is built by information contained in /boot/grub/grub.cfg.

Installation Options (LiveCD)

Using the Entire Drive ( "Erase disk and install Ubuntu" ):

Install on entire drive: The user should select a drive on which Ubuntu and GRUB 2 will be installed. GRUB 2 will overwrite the drive's boot information and place the GRUB 2 files in the appropriate locations.

Installing Ubuntu to a Specific Partition ("Something Else"):

When using the "Something Else" option, you will be offered to choose the "Device for bootloader installation". Please select: * either the disk (eg /dev/sdX, not /dev/sdXY) on which the BIOS is setup to boot (recommended for normal use) * OR the partition (eg /dev/sdXY, not /dev/sdX) on which Ubuntu (/boot, else /) will be installed (only if you want to chainload it from another bootloader; if any doubt, do NOT choose this)

important.png Never choose any other partition! (this may break the boot of your other systems, see Bug #1049549).

g2_installing_partition.png

info.png On a system with multiple drives and OS's, the user can preserve the original bootloader by installing GRUB 2 on another drive. To accomplish this:

  • specify the disk (eg /dev/sdX, not /dev/sdaXY) not currently used to boot the system for the bootloader location.
  • After the installation is complete, change the boot order (via BIOS setup) so that the disk to which the GRUB information was written is the one booted first.
  • If the user wishes to restore booting with the original bootloader, change the boot order back to the original drive.

BIOS/MBR Notes

GRUB 2 normally writes its bootloader information to an area just beyond the Master Boot Record and before the first partition. Some software on non-Linux operating systems also used this embedded area and broke the system by overwriting the GRUB 2 information on dual-boot systems. GRUB 2 now can recognize this potential problem and avoid it.

The bootloader information does not have to be installed to the embedded area of the MBR. It may also bypass the MBR entirely and be installed to a specific partition. In doing this, the location of the GRUB 2 files are specified by using blocklists. This option is not available via an Ubuntu GUI installation but can be made via the terminal after installation. Even then this method is not as reliable as writing to the MBR and is not recommended by the GRUB developers.

BIOS/GPT Notes

If the BIOS is setup to boot the disk in Legacy/mbr mode, installing GRUB2 on a GPT (GUID Partition Table) disk requires a dedicated BIOS boot partition with a recommended size of at least 1 MiB. This partition can be created via GParted or other partitioning tools, or via the command line. It must be identified with a bios_grub flag. The necessary GPT modules are automatically included during installation when GRUB 2 detects a GPT scheme.

UEFI

GRUB 2 is compatible with EFI/UEFI and will load the necessary modules during installation. For information on this topic, please visit the UEFI community documentation.

Post-Installation

At the conclusion of a normal installation using the default entries:

  • GRUB 2 will be the default bootloader for the computer.
    • GRUB 2 will have written information to the designated drive's boot area pointing to the Ubuntu installation's GRUB 2 files. On earlier versions of GRUB 2 on a BIOS systems any information previously contained in the area just beyond MBR will have been overwritten. An improvement to GRUB 2 allows it to write the boot information to another area if a non-Linux application has already used this area.
    • The GRUB 2 files will normally be located in the /boot/grub and /etc/grub.d folders and the /etc/default/grub file in the partition containing the Ubuntu installation.

    • If another Ubuntu/Linux distribution controlled the boot process, it will be replaced by the GRUB 2 settings in the new installation.
    • If the boot was controlled by Windows, the MBR information will now point to the Ubuntu installation and GRUB 2 will control the boot process. The GRUB 2 menu will include an entry for Windows if it found what it considers the necessary Windows boot files.
      • Windows boot files in the Windows partition are not affected by the GRUB 2 installation. Only the MBR data is changed. Windows control of the boot process can be obtained by selecting a Windows entry in the GRUB 2 menu during boot.
  • If GRUB has detected another OS it will display a menu so the user may make a selection. If no selection is made, Ubuntu will boot after a brief timeout.
  • If GRUB has detected no other operating systems, no menu will be displayed and the computer will boot the new Ubuntu installation. If the user wishes to display the menu, hold down the SHIFT key as the computer boots.

To view the GRUB 2 file system and configuration options please refer to Grub 2 community documentation page.

Reinstalling GRUB 2

The user may wish to reinstall GRUB 2 for a variety of reasons, including:

  • After installing another operating system which installed its own bootloader, reinstall GRUB2 to return control to the desired OS. When an operating system is added on a multiboot computer it will generally take command of the boot process. Reinstalling GRUB 2 from a within the desired OS will return control of the boot process to that OS.
  • Add the boot information to an additional drive. GRUB 2 can be installed on multiple drives - all pointing to the same operating system. This provides a backup in case the MBR or boot record data on a particular drive becomes corrupted.
  • Replace missing files
  • Repair a broken system, such as when the user sees only "GRUB" on a black screen with no prompt or when the user cannot boot using GRUB 2's rescue mode options.
  • Upgrade from Grub Legacy

When using the grub-install command, the boot information is updated and written to the designated drive, missing - but not corrupted or intentionally deleted - files are restored. Specifically the core.img, grubenv, and device.map are updated and missing modules restored. If missing, the grub folder will be recreated. The grub-install command does not generate a new GRUB 2 menu (grub.cfg).

Reinstalling GRUB 2 from a Working System

If Ubuntu is operating normally, boot into the working installation and run the following command from a terminal.

  • X is the drive (letter) on which you want GRUB to write the boot information. Normally users should not include a partition number, which would produce an error message as the command would attempt to write the information to a partition.

    sudo grub-install /dev/sdX  # Example: sudo grub-install /dev/sda

This will rewrite the MBR information to point to the current installation and rewrite some GRUB 2 files (which are already working). Since it isn't done during execution of the previous command, running sudo update-grub after the install will ensure GRUB 2's menu is up-to-date.

Fixing a Broken System

If a system has become unbootable due to a GRUB 2 failure or corrupted MBR/boot sector, there are various methods for reinstalling GRUB 2. The simplest is listed first, followed by more complicated methods.

via Boot-Repair Graphical Tool

Boot-Repair is a GUI application which can fix a variety of GRUB 2 problems. It can be used either from a LiveCD, its own CD, or during a normal Ubuntu session. In addition to providing a simple user interface, Boot-Repair also includes the ability to run a script to gather details of the system which can be used for troubleshooting help on various forums and IRC channels.

Please refer to the Boot-Repair community documentation for instructions on how to install and run this application.

via GRUB2 Rescue mode

GRUB 2 provides a rescue mode which allows the user to attempt to repair GRUB 2 from a special GRUB terminal during boot. Refer to the Grub2 Command_Line_and_Rescue_Mode section for information on GRUB terminal operations.

via the LiveCD terminal

This method uses the terminal from a LiveCD. The user must know the device name/partition of the installed system (sda1, sdb5, etc), which is then mounted from the LiveCD. GRUB 2 files are copied from the LiveCD libraries to the proper locations and MBR.

This operation will write to the MBR and restore the modules and core.img to /boot/grub. It will not replace or restore grub.cfg or fix corrupted files. Boot the LiveCD, select "Try It", and open a terminal.

info.png When using a LiveCD, due to GRUB 2 changes between Ubuntu releases, it is recommended that the user boots a LiveCD of the same release (11.10, 12.04, etc) as the release to be repaired. If the user has installed a different version of GRUB 2, use a LiveCD with the same GRUB 2 version.

If necessary, use the fdisk command to help determine the partition on which Ubuntu is installed. The fdisk option "-l" is a lowercase "L". Look for one of the appropriate size or formatting. Running sudo blkid may provide more information to help locate the proper partition, especially if the partitions are labeled. The '

  • sudo fdisk -l
    sudo blkid

In the following commands:

  • Use the partition number of the Ubuntu installation with mount command.

  • Do not use the partition number with the grub-install command.

  • X is the drive letter (a, b, c, etc.); Y is the partition number (1, 5, etc).

  • --boot-directory is the folder in which the GRUB folder is located. This is normally /boot but should be changed if the grub folder is located elsewhere.

  • On systems with a separate /boot partition, that partition should be mounted to /mnt/boot. For instance: sudo mount /dev/sda6 /mnt/boot

  • grub-install will restore missing files in the grub folder but will not restore intentionally deleted or corrupted files. To accomplish these tasks GRUB 2 must be completely removed and reinstalled.

    sudo mount /dev/sdXY /mnt # Example: sudo mount /dev/sda5 /mnt
    sudo grub-install --boot-directory=/mnt/boot /dev/sdX # Example: sudo grub-install --boot-directory=/mnt/boot /dev/sda

    If Ubuntu is installed on B-tree file system i.e. btrfs then /boot changes to /@/boot in above commands such that :

    sudo grub-install --boot-directory=/mnt/@/boot /dev/sdX # Example: sudo grub-install --boot-directory=/mnt/@/boot /dev/sda

    For GRUB 2 version 1.98 and earlier (Ubuntu 10.04), the installation command is slightly different. Rather than designating the --boot-directory, the switch is --root-directory. In this case, the location changes to /mnt

    sudo grub-install --root-directory=/mnt /dev/sdX

If the user wishes to explore why the system failed, refer to Post-Restoration Commands section below.

via Partition Files Copy

This method does not work if GRUB has not already been installed, or if GRUB files have been deleted.

important.png This method talks about 9.10, so it may be obsolete.

It consists in locating and mounting the problem boot partition from the LiveCD. The files are then copied from the broken system's /boot/grub directory to the proper locations and MBR. It requires fewer steps and fewer command line entries than the following CHROOT method. For users with separate system partitions, such as a separate /boot partition, or other special circumstances, using the CHROOT method may provide better results.

  1. Boot to the LiveCD Desktop.
  2. Mount the partition with your Ubuntu installation.
    1. From the Places menu, select and click the partition containing your Ubuntu installation to mount it.

      • grub2.places.devices.png

      • If the partition isn't recognized, look for apartition of the appropriate size or label.
      • Once mounted, the user should see Ubuntu system directories such as /boot

  3. Open a terminal by selecting Applications, Accessories, Terminal from the menu bar.

  4. Run the grub-setup -d command as described below. This will reinstall the GRUB 2 files on the mounted partition to the proper location and to the MBR of the designated device.

    1. Determine the mount point by referring to the location box in Places. Replace XXXX in the command with the UUID, or replace /media/XXXX with the correct location if the partition is not mounted in /media. The designation may be a UUID, or a label, if the user has created one (see note).

    2. If the location window is not in the format shown, click on the icon to the left to change the presentation.
      • grub2.places.location.png

    3. When ready to copy the UUID/location, by highlighting it with the mouse. Paste it into the terminal by pressing the middle mouse button. If using the keyboard, use CTRL-SHIFT-arrow to select and CTRL-C to copy the location and CTRL-SHIFT-V to paste it into the terminal.

    4. The device/drive is designated by sdX, with X being the device designation. sda is the first device, sdb is the second, etc. For most users the MBR should be installed to sda, the first drive on their system.

      sudo grub-setup -d /media/XXXX/boot/grub /dev/sda
      Example:

      sudo grub-setup -d /media/7848138a-41a0-4eba-8aed-d1b625ac8759/boot/grub /dev/sda

    5. If the user gets a "error: Cannot open /boot/grub/device.map" message, add the -m switch and path to the device.map to the command as follows:

      sudo grub-setup -d /media/XXXXX/boot/grub -m /media/XXXXX/boot/grub/device.map /dev/sda
      Example:

      sudo grub-setup -d /media/7848138a-41a0-4eba-8aed-d1b625ac8759/boot/grub -m /media/7848138a-41a0-4eba-8aed-d1b625ac8759/boot/grub/device.map /dev/sda

    6. Other switches are available for use with the grub-setup command. In a terminal, type grub-setup --help for a list of available options.

    7. You may find you get a repeated "error: Cannot open /boot/grub/device.map" message. If this happens try rebooting. The UUID method may actually work but throws an error message falsely. Using the label name does not seem to evoke the same repeated error message.

  5. Reboot
  6. Refresh the GRUB 2 menu with sudo update-grub

  7. If the user wishes to explore why the system failed, refer to Post-Restoration Commands section below.

    info.png Disk Utility, introduced in Ubuntu 9.10, can easily assign labels to existing partitions. Access Disk Utility from the System, Administration menu or DASH.

via ChRoot

This method of installation uses the chroot command to gain access to the broken system's files. Once the chroot command is issued, the LiveCD treats the broken system's / as its own. Commands run in a chroot environment will affect the broken systems filesystems and not those of the LiveCD.

  1. Boot to the LiveCD Desktop. The CD should be 64bit (not 32bit) if the system to fix is 64 bit.
  2. Open a terminal. The method varies depending on the release.
  3. Assuming the normal system partition(s) are on a software RAID (otherwise skip this step): make sure the mdadm tools are installed in the Live CD environment (e.g. by executing sudo apt-get install mdadm). Then assemble the arrays:

    sudo mdadm --assemble --scan
  4. If using LVM:

    sudo sh -ec "apt-get install lvm2; vgchange -ay"
  5. If using bcache:

    sudo sh -ec "apt-get install software-properties-common; add-apt-repository ppa:g2p/storage; apt-get update; apt-get install bcache-tools"
  6. Determine your normal system partition. The following commands may be helpful. The fdisk switch is a lowercase "L".

    sudo fdisk -l
    sudo blkid
    df -Th
  7. Mount your normal system partition. X is the drive letter. Y is the partition number:

    • Substitute the correct partition: sda1, sdb5, etc.
    sudo mount /dev/sdXY /mnt
    • Example 1: sudo mount /dev/sda1 /mnt

    • Example 2: sudo mount /dev/md1 /mnt

  8. Only if you have a separate boot partition (where sdXY is the /boot partition designation):

    sudo mount /dev/sdXY /mnt/boot
    • Example 1: sudo mount /dev/sdb6 /mnt/boot

    • Example 2: sudo mount /dev/md0 /mnt/boot

  9. Only if (some) of the system partitions are on a software RAID (otherwise skip this step): make sure the output of mdadm --examine --scan agrees with the array definitions in /etc/mdadm/mdadm.conf.

  10. Mount the critical virtual filesystems. Run the following as a single command:
    for i in /dev /dev/pts /proc /sys /run; do sudo mount -B $i /mnt/$i; done
  11. Chroot into your normal system device:
    sudo chroot /mnt
  12. Reinstall GRUB 2 (substitute the correct device with sda, sdb, etc. Do not specify a partition number):

    grub-install /dev/sdX

    If the system partitions are on a software RAID install GRUB 2 on all disks in the RAID. Example (software RAID using /dev/sda and /dev/sdb):

    grub-install /dev/sda
    grub-install /dev/sdb
  13. Recreate the GRUB 2 menu file (grub.cfg)

    update-grub
  14. Exit chroot: CTRL-D on keyboard

  1. Reboot.
    • sudo reboot

Boot repair after a Windows Upgrade on Ubuntu 14.04 (non-RAID)

Boot repair may be required when updating Windows (e.g. Windows 8 to Windows 8.1). This will be required for example if the computer has been configured such that Windows/Ubuntu is selected by entering the bios and changing the boot mode between "secure boot"/UEFI and CMS respectively. To manually repair grub (http://howtoubuntu.org/how-to-repair-restore-reinstall-grub-2-with-a-ubuntu-live-cd / http://askubuntu.com/questions/88384/how-can-i-repair-grub-how-to-get-ubuntu-back-after-installing-windows );

  • Insert Ubuntu installation media (e.g. Ubuntu 14.04 LTS 64 bit DVD)
  • Ensure the bios boot order is set to read the media first (consult your motherboard/computer manual) and restart the computer
  • Select Ubuntu Live/Try mode (do not install)
  • Click on the purple Ubuntu search button - enter "gparted" and click on its icon
  • Determine the name of your linux partition (/dev/sdXY) - you should be able to identify it by recognising its format (most likely ext4) and size (number of GB)
  • Click on purple Ubuntu search button - enter "gnome-terminal" and click on its icon
  •  sudo mount /dev/sdXY /mnt [where sdXY has previously been identified using gparted]

  •  for i in /dev /dev/pts /proc /sys /run; do sudo mount -B $i /mnt$i; done

  •  sudo chroot /mnt

  •  sudo grub-install /dev/sdX [note this is sdX not sdXY!]

  •  update-grub

  •  exit

  • restart PC and remove Ubuntu installation media

Purging & Reinstalling GRUB 2

The difference between using the grub-install command and purging & reinstalling GRUB 2 is that the latter completely removes all GRUB 2 files and system settings. Purging GRUB 2 is also a method to reset all configuration settings to the default values on a working system. This may correct GRUB 2 failures due to corrupted files and improper configurations as well as for missing folders/files deleted by the user. The GRUB 2 packages removed and reinstalled via this procedure include grub-pc, grub-common and, in Grub 1.99 and later, grub-gfxpayload-lists.

important.png During this operation the user will be temporarily left with no bootloader. Ensure you have a working Internet connection and access to the repositories before purging the GRUB 2 packages.

This procedure can be accomplished very simply via Boot-Repair, or via the terminal procedure below.

Remark: the Boot-Repair procedure will also remove custom files that may block the boot. It is also able to purge and reinstall GRUB2 for UEFI / LVM / RAID systems.

via Boot-Repair Graphical Tool

* Run Boot-Repair either from liveCD or from your installed session * Click "Advanced options" --> "GRUB options" tab * Tick the "Purge GRUB and reinstall it" option --> Apply

via Terminal Commands

The commands from a working Ubuntu installation or once within the ChRoot of a LiveCD are the same.

  1. If using the LiveCD, complete the steps up to and including the sudo chroot command (Step 9) in the previous section.

Run the following commands as root. If not in a ChRoot environment, preface each command with sudo.

  1. Update the repositories to get a list of the most recent packages available. Ensure you have a working Internet connection and access to the repositories. If you do not, STOP!
    apt-get update
  2. The following command will completely remove grub-pc and grub-common. In GRUB 1.99 and later, grub-gfxpayload-lists will also be removed.

    • You will be warned you are removing the bootloader. TAB to OK and press ENTER.

    apt-get purge grub-common
  3. Reinstall GRUB 2. When you run the following command:
    • You will be given the opportunity to add kernel options. TAB to OK. You will be given the opportunity to select the devices/drives on which to install GRUB 2. Select the appropriate drive(s) with the SPACEBAR. Normally you do NOT want to select any partitions. TAB to OK after selecting the drive(s), then press ENTER.
    apt-get install grub-pc
  4. If using the ChRoot procedure, Exit chroot then continue the ChRoot procedure at Step 13:

Post-Restoration Commands

Once the user can boot to a working system, the following commands may prove useful in locating and/or fixing the problem.

  • To refresh the available devices and settings in /boot/grub/grub.cfg

    • sudo update-grub

    To look for the bootloader location.
    • grub-probe -t device /boot/grub

    To install GRUB 2 to the sdX drive's MBR or boot sector (sda, sdb, etc.)

    • sudo grub-install /dev/sdX

Moving GRUB 2

The command to change the GRUB 2 installation device or boot files is grub-install run as root. This command allows the user to modify the installation by setting the ROOT directory, preload modules, run specific setup files and more. When executed, grub-install may run one or more other commands, such as grub-probe, grub-mkimage, and grub-setup. Here are some considerations when running grub-install:

  • The grub-install command should be used rather than grub-setup under normal circumstances. grub-setup will be called by grub-install when needed.

  • The command should specify a device and when executed will install the required GRUB files to the location called for in the options.
    • If only the device is specified (sda, sdb, etc), the boot information will be written to the specified device's boot record. (example: sudo grub-install /dev/sda )

    • If the --boot-directory= switch is used, the grub folder will will become a subfolder of the location specified. The --boot-directory switch is available in GRUB 1.99 and later.

  • If the user attempts to run the command with a specific partition (example: sudo grub-install /dev/sda6 ) a warning will be issued. Specifying a partition is not recommended due to the use of blocklists, which the developers consider unreliable. An option is provided on how to override this recommendation if the user still wishes to do so.

  • The list of options available for grub-install can be displayed in a terminal with grub-install --help

  • An alternative command to install or move GRUB 2 from a particular device is to use the dpkg-reconfigure command. As the command runs, the user will be offered the opportunity to add kernel options on the first page and to specify the devices/drives on the second screen. Use the TAB key to highlight OK and press ENTER to continue.

    • Add the desired kernel options (such as 'noapic'), TAB to OK when finished, and press ENTER.

    • Select the drive(s) with the SPACEBAR. It is not recommended to select any partition. TAB to OK when finished and press ENTER.

GRUB Legacy Upgrades and Downgrades

For information on how to switch from GRUB Legacy to GRUB 2, or from GRUB 2 to GRUB Legacy, please refer to the Grub2/Upgrading community documentation.


Related Links

Grub2

Grub2/Submenus

Grub2/Upgrading

Boot-Repair

GNU GRUB Manual


CategoryBootAndPartition CategoryBootAndPartition

Grub2/Installing (last edited 2021-12-22 04:59:36 by sambrightman)